<?php include(VIEWSPATH."/common/header.php") ?>
<script>
	$(document).ready(function(){
		$("#btn_create").click(function(){
			update("create");
	  	});
		$("#btn_update").click(function(){
			update("update");
			$("input[type=text]").val("");
			$("#btn_create").show();
			$("#btn_update").hide();
			$("#btn_cancel").hide();
	  	});
	  	$("#btn_cancel").click(function(){
	  		$("#btn_create").show();
			$("#btn_update").hide();
			$("#btn_cancel").hide();
			$("input[type=text]").val("");
	  	});
		$("#btn_delete").click(function(){
			var ids = "";
			$("input[type=checkbox]:checked").each(function(){
				ids = ids + this.value +",";
			});
			if (ids !=""){
				
				ids = ids.substring(0,ids.length -1);
				
				$.ajax({
					url:"<?php echo base_url('product/delete_theloai');?>",
					data:{
						'id': ids 
					},
					 type: "POST",
					success:function(data){
						if(data){
	    		  			$("#list_type").html(data);
	  	  				}
					}
				});
			}
		});
	});
	function update(type){
		$.post("<?php echo base_url('product/theloai')?>",
  			{
  				'name':$("#txt_name").val(),
  				'name_short': $("#txt_name_short").val() ,
  				'type':type,
  				'id':$("#hdf_id").val()
  			},
  			function(data){
  	  			if(data){
    		  			$("#list_type").html(data);
  	  			}
  	  			
  			});
	  	}
  function load(str){
	  var arr = str.split("#");
	  $("#hdf_id").val(arr[0]);
	  $("#txt_name").val(arr[1]);
	  $("#txt_name_short").val(arr[2]);
	  $("#btn_create").hide();
	  $("#btn_update").show();
	  $("#btn_cancel").show();
	  
  }
	
</script>

<style>
	#area_laysodomoi table td{
		text-align:left;
		}
</style>


<div id="area_laysodomoi">
	<table width ="400">
		<tr>
			<td>Tên</td>
			<td><input type="text" name="txt_name" id="txt_name"></input></td>
		</tr>
		<tr>
			<td>Tên ngắn</td>
			<td><input type="text" name="txt_name_short" id="txt_name_short" ></input></td>
		</tr>
		<tr>
			<td colspan="2" style="text-align:center;">
				<input type="submit" value="Thêm" id="btn_create" > </input>
				<input type="submit" value="Thay Đổi" id="btn_update" style="display:none;"> </input>
				<input type="submit" value="Bỏ qua" id="btn_cancel" style="display:none;"> </input>
				<input type="hidden" id="hdf_id" > </input>
			</td>
		
		</tr>
	</table>
</div>
<div style="text-align:left;padding-bottom:5px;">
	<input type="button" value="Xoá" id="btn_delete" ></input>
</div>
<div id="list_type" >
	<table width="300" class="list" >
	<tr>
		<th></th>
		<th>ID</th>
		<th>Tên</th>
		<th>Tên Tiếng Anh</th>
		<th>Tên Ngan</th>
		<th></th>
	</tr>
	<?php foreach ($info as  $row ){?>
	<tr>
		<td align="center" ><input type="checkbox" value ="<?php echo $row -> id ?>" id="ckb_<?php echo $row -> id ?>"></input>
		<td><?php echo $row->id ?></td>
		<td><?php echo $row->name ?></td>
		<td><?php echo $row->name_eng ?></td>
		<td><?php echo $row->name_short ?></td>
		<td><span class="link" onclick="load('<?php echo $row -> id ."#".$row->name."#".$row->name_short ?>')">Sửa</span></td>
	</tr>
	<?php }?>
</table>
</div>

